package com.xbongbong.workflow.service.field.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.FilterCondition;
import com.xbongbong.workflow.pojo.WorkflowRulePojo;
import com.xbongbong.workflow.service.field.AbstractWorkflowConditionFieldService;
import com.xbongbong.workflow.service.field.WorkflowConditionFieldService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/07 14:03
 */
@Service("workflowConditionComboCheckServiceImpl")
public class WorkflowConditionComboCheckServiceImpl extends AbstractWorkflowConditionFieldService implements WorkflowConditionFieldService {


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO_CHECK.getType(), FieldTypeEnum.CHECKBOX_GROUP.getType());
    }



    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowNodeEntity workflowEntity, FilterCondition workflowConditionPojo) throws XbbException {
        Object object = workflowConditionPojo.getValue();
        if (object instanceof String) {
            String value = String.valueOf(object);
            List<String> valueList;
            if (value.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueArray = value.split(PaasConstant.CHINESE_SEMICOLON);
                valueList = Arrays.asList(valueArray);
            } else {
                valueList = Arrays.asList(value);
            }
            // 复选框/下拉复选
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                object = ItemUtil.getCustomItemList(targetField, valueList);
            } else {
                object = valueList;
            }
        } else if (object instanceof List) {
            List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(object), ItemPoJo.class);
            List<Object> valueList = new ArrayList<>();
            itemList.forEach(item->{
                valueList.add(item.getValue());
            });
            object = valueList;
        }
        return object;
    }


    @Override
    public Object handlerThisFormField(PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                       WorkflowNodeEntity workflowNodeEntity, FilterCondition filterCondition, WorkflowRulePojo workflowRulePojo) throws XbbException {
        // 根据value获取text
        // 1、如果本表单字段是下拉框、下拉复选、单选按钮、复选按钮，需要先根据value取出text，放入data中；
        // 2、在下方取出data的text，然后拿到对应的value，之后在根据value去查询对应的数据；
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerTargetFormField(Object object, PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                         WorkflowNodeEntity workflowNodeEntity, WorkflowRulePojo workflowRulePojo, FilterCondition filterCondition) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
            List<String> list = JSON.parseArray(JSON.toJSONString(object), String.class);
            return ItemUtil.getCustomItemList(targetField, list);
        } else {
            return object;
        }
    }
}
